<?php 

require dirname(dirname(__FILE__)).'/class_online/conf.php';

class RemoteMethod{
	private $host = 'http://111.229.44.50';

	public function __construct() {
		
	}
	
	public function genK($param){
	    if(empty($param['appid'])||empty($param['stream'])){
	        return array(
	            'Flag'=>101,
	            'FlagString'=>'参数错误'
	        );
	    }
	    $t = time();
	    $k = md5($param['appid'].$param['stream'].WisConf::ykToken.$t);
	    $result = array('Flag'=>100);
	    $result['k'] = $k;
	    $result['t'] = $t;
	    return $result;
	}
	
	public function s_url($param){
	    $result = array('Flag'=>100);
	    $result['rtmp_publish_url'] = WisConf::rtmp_publish_url;
	    $result['rtmp_play_url'] = WisConf::rtmp_play_url;
	    $result['hls_play_url'] = WisConf::hls_play_url;
	    return $result;
	}
	
	public function u_c_t($param){
	    if(empty($param['classid'])||empty($param['userid'])){
	        return array(
	            'Flag'=>101,
	            'FlagString'=>'参数错误'
	        );
	    }
	    $result = $this->invoke("/YCJsonService.aspx?key=7C7ABC56-1A09-448c-9F5C-FBC464EC7DAB&method=104&classid=".$param['classid']."&userid=".$param['userid']);
	    // $result['Flag'] = 100;
	    // $result['Info']['result'] = array();
	    
	    $result['rtmp_publish_url'] = WisConf::rtmp_publish_url;
	    $result['rtmp_play_url'] = WisConf::rtmp_play_url;
	    $result['hls_play_url'] = WisConf::hls_play_url;
	    $result['dmsPubKey'] = WisConf::dmsPubKey;
	    $result['dmsSubKey'] = WisConf::dmsSubKey;
	    return $result;
	}
	
	public function u_c($param){
	    if(empty($param['classid'])||empty($param['userid'])){
	        return array(
	            'Flag'=>101,
	            'FlagString'=>'参数错误'
	        );
	    }
	    $result = $this->invoke("/YCJsonService.aspx?key=7C7ABC56-1A09-448c-9F5C-FBC464EC7DAB&method=103&classid=".$param['classid']."&userid=".$param['userid']);
	    //$result['Flag'] = 100;
	    //$result['Info']['result'] = array();
	    
	    $result['rtmp_publish_url'] = WisConf::rtmp_publish_url;
	    $result['rtmp_play_url'] = WisConf::rtmp_play_url;
	    $result['hls_play_url'] = WisConf::hls_play_url;
	    $result['dmsPubKey'] = WisConf::dmsPubKey;
	    $result['dmsSubKey'] = WisConf::dmsSubKey;
	    return $result;
	}
	
	public function get_lang(){
	    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4);
	    $result = array('Flag'=>100);
	    if (preg_match("/zh-c/i", $lang))
	        $result['lang'] = "zh";
	    else if (preg_match("/zh/i", $lang))
	        $result['lang'] = "zh";
	    else if (preg_match("/en/i", $lang))
	        $result['lang'] = "en";
	    else
	        $result['lang'] = "zh";
	    return $result;
	}
	
	public function getClassDocs($param){
	    if(empty($param['classid'])){
	        return array(
	            'Flag'=>101,
	            'FlagString'=>'参数错误'
	        );
	    }
	    return $this->invoke("/YCJsonService.aspx?key=7C7ABC56-1A09-448c-9F5C-FBC464EC7DAB&method=101&classid=".$param['classid']);
	    // $result['Flag'] = 100;
	    // $result['Info']['result'] = array(array('docid'=>'49221b438a259449c71a4fcdbae3bc73','docname'=>'空白文档'),array('docid'=>'5aa010d94f818cfae79ebe5447660816','docname'=>'语文'),array('docid'=>'b076817e8ef710cd15aecab7298e1b5c','docname'=>'数学'));
	    
	    // return $result;
	}
	
	public function getClassMp3s($param){
	    if(empty($param['classid'])){
	        return array(
	            'Flag'=>101,
	            'FlagString'=>'参数错误'
	        );
	    }
	    return $this->invoke("/YCJsonService.aspx?key=7C7ABC56-1A09-448c-9F5C-FBC464EC7DAB&method=105&classid=".$param['classid']);
	    // $result['Flag'] = 100;
	    // $result['Info']['result'] = array();
	    
	    // return $result;
	}
		
	private function invoke($path,$method="GET",$content="",$expire=3600,$timeout=60){
	    $ch = curl_init();
	    $expire = time() + $expire;
	    $query_url = $this->host.$path;
	    
	    curl_setopt($ch,CURLOPT_URL,$query_url);
	    curl_setopt($ch,CURLOPT_HEADER,false);
	    
	    curl_setopt($ch,CURLOPT_AUTOREFERER,true);
	    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
	    curl_setopt($ch,CURLOPT_FRESH_CONNECT,true);
	    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
	    curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
	    //	curl_setopt($ch,CURLOPT_USERAGENT,$useragent);
	    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	    curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
	    curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$method);
	    // curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	    //print_r($authorization);exit();
	    $header = array('AD-Expire: '.$expire,'Content-Type: application/json');
	    curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
	    //execute post
	    $response = curl_exec($ch);
	    //get response code
	    $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	    //close connection
	    curl_close($ch);
	    //return result
	    
	    if($response_code == 200 || $response_code == 201 || $response_code == 204) {
	        	
	        $rst = array(
	            'Flag'=>100,
	            'FlagString'=>'success',
	            'Info'=>json_decode(trim($response),true)
	        );
	    } else {
	        $rst = array(
	            'Flag'=>110,
	            'FlagString'=>'faile',
	            'Info'=>trim($response)
	        );
	        
	    }
	    return $rst;
	}
	
}
?>